<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="支付类型" prop="stoPay">
        <el-input
            v-model="queryParams.stoPay"
            placeholder="请输入支付类型"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进货时间" prop="stoDate">
        <el-date-picker clearable
                        v-model="queryParams.stoDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择进货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="操作者编号" prop="stoActor">
        <el-input
            v-model="queryParams.stoActor"
            placeholder="请输入操作者编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="stoNote">
        <el-input
            v-model="queryParams.stoNote"
            placeholder="请输入备注"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批人编号" prop="appMangerId">
        <el-input
            v-model="queryParams.appMangerId"
            placeholder="请输入审批人编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批时间" prop="appPassdate">
        <el-date-picker clearable
                        v-model="queryParams.appPassdate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择审批时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="stoState">
        <el-input
            v-model="queryParams.stoState"
            placeholder="请输入订单状态"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:sto:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:sto:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:sto:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:sto:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="stoProId" />
      <el-table-column label="供应商编号" align="center" prop="supId" />
      <el-table-column label="支付类型" align="center" prop="stoPay" />
      <el-table-column label="进货时间" align="center" prop="stoDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.stoDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作者编号" align="center" prop="stoActor" />
      <el-table-column label="备注" align="center" prop="stoNote" />
      <el-table-column label="审批人编号" align="center" prop="appMangerId" />
      <el-table-column label="审批时间" align="center" prop="appPassdate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.appPassdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="stoState" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:sto:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:sto:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改采购订单管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="stoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="支付类型" prop="stoPay">
          <el-input v-model="form.stoPay" placeholder="请输入支付类型" />
        </el-form-item>
        <el-form-item label="进货时间" prop="stoDate">
          <el-date-picker clearable
                          v-model="form.stoDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择进货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作者编号" prop="stoActor">
          <el-input v-model="form.stoActor" placeholder="请输入操作者编号" />
        </el-form-item>
        <el-form-item label="备注" prop="stoNote">
          <el-input v-model="form.stoNote" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="审批人编号" prop="appMangerId">
          <el-input v-model="form.appMangerId" placeholder="请输入审批人编号" />
        </el-form-item>
        <el-form-item label="审批时间" prop="appPassdate">
          <el-date-picker clearable
                          v-model="form.appPassdate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择审批时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单状态" prop="stoState">
          <el-input v-model="form.stoState" placeholder="请输入订单状态" />
        </el-form-item>
        <el-divider content-position="center">供应商信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddSup">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteSup">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="supList" :row-class-name="rowSupIndex" @selection-change="handleSupSelectionChange" ref="sup">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="供应商名称" prop="supName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supName" placeholder="请输入供应商名称" />
            </template>
          </el-table-column>
          <el-table-column label="供应商地址" prop="supAddress" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supAddress" placeholder="请输入供应商地址" />
            </template>
          </el-table-column>
          <el-table-column label="供应商邮箱" prop="supEmail" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supEmail" placeholder="请输入供应商邮箱" />
            </template>
          </el-table-column>
          <el-table-column label="联系人" prop="supContact" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supContact" placeholder="请输入联系人" />
            </template>
          </el-table-column>
          <el-table-column label="联系电话" prop="supTel" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supTel" placeholder="请输入联系电话" />
            </template>
          </el-table-column>
          <el-table-column label="开户银行" prop="supBank" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supBank" placeholder="请输入开户银行" />
            </template>
          </el-table-column>
          <el-table-column label="开户行账号" prop="supAccount" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supAccount" placeholder="请输入开户行账号" />
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="supState" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supState" placeholder="请输入状态" />
            </template>
          </el-table-column>
          <el-table-column label="供应商类型" prop="supType" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.supType" placeholder="请选择供应商类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Sto">
import { listSto, getSto, delSto, addSto, updateSto } from "@/api/system/sto";

const { proxy } = getCurrentInstance();

const stoList = ref([]);
const supList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedSup = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supId: null,
    stoPay: null,
    stoDate: null,
    stoActor: null,
    stoNote: null,
    appMangerId: null,
    appPassdate: null,
    stoState: null
  },
  rules: {
    supId: [
      { required: true, message: "供应商编号不能为空", trigger: "change" }
    ],
    stoPay: [
      { required: true, message: "支付类型不能为空", trigger: "blur" }
    ],
    stoActor: [
      { required: true, message: "操作者编号不能为空", trigger: "blur" }
    ],
    appMangerId: [
      { required: true, message: "审批人编号不能为空", trigger: "blur" }
    ],
    stoState: [
      { required: true, message: "订单状态不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询采购订单管理列表 */
function getList() {
  loading.value = true;
  listSto(queryParams.value).then(response => {
    stoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    stoProId: null,
    supId: null,
    stoPay: null,
    stoDate: null,
    stoActor: null,
    stoNote: null,
    appMangerId: null,
    appPassdate: null,
    stoState: null
  };
  supList.value = [];
  proxy.resetForm("stoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.stoProId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加采购订单管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _stoProId = row.stoProId || ids.value
  getSto(_stoProId).then(response => {
    form.value = response.data;
    supList.value = response.data.supList;
    open.value = true;
    title.value = "修改采购订单管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["stoRef"].validate(valid => {
    if (valid) {
      form.value.supList = supList.value;
      if (form.value.stoProId != null) {
        updateSto(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSto(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _stoProIds = row.stoProId || ids.value;
  proxy.$modal.confirm('是否确认删除采购订单管理编号为"' + _stoProIds + '"的数据项？').then(function() {
    return delSto(_stoProIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 供应商序号 */
function rowSupIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 供应商添加按钮操作 */
function handleAddSup() {
  let obj = {};
  obj.supName = "";
  obj.supAddress = "";
  obj.supEmail = "";
  obj.supContact = "";
  obj.supTel = "";
  obj.supBank = "";
  obj.supAccount = "";
  obj.supState = "";
  obj.supType = "";
  supList.value.push(obj);
}

/** 供应商删除按钮操作 */
function handleDeleteSup() {
  if (checkedSup.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的供应商数据");
  } else {
    const sups = supList.value;
    const checkedSups = checkedSup.value;
    supList.value = sups.filter(function(item) {
      return checkedSups.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleSupSelectionChange(selection) {
  checkedSup.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/sto/export', {
    ...queryParams.value
  }, `sto_${new Date().getTime()}.xlsx`)
}

getList();
</script>
